﻿using System.Web.Mvc;
using ProjectFootball.MVC.Areas.AccountArea.Controllers.Foundation;
using ProjectFootball.MVC.Areas.AccountArea.Models;
using WebMatrix.WebData;

namespace ProjectFootball.MVC.Areas.AccountArea.Controllers
{
    public class LoginController : BaseAccountController
    {
        [AllowAnonymous]
        public ActionResult Index(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Index(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            WebSecurity.Logout();

            return RedirectToAction("Index", "Home", new { area = "" });
        }
    }
}
